package com.zxyankh.raft.repository;

import com.zxyankh.raft.domain.LogEntryDomain;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author ankh
 * @since 31.10.2017
 */
public interface LogRepository extends JpaRepository<LogEntryDomain, Integer>, JpaSpecificationExecutor<LogEntryDomain> {

    @Query(value = "SELECT id,term,command,server FROM logs WHERE term=?1 AND server=?3 AND index=?2", nativeQuery = true)
    LogEntryDomain findLogEntry(int term, int index, int server);

    @Query(value = "SELECT id, term, command, server FROM logs WHERE index=?1", nativeQuery = true)
    LogEntryDomain findLogEntryByIndex(int index);

    @Query(value = "SELECT id,term,command,server FROM logs WHERE server=?1", nativeQuery = true)
    List<LogEntryDomain> findLogEntries(int server);

}
